package com.yuyou.fn.educational.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.CriteriaUtils;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.educational.entity.Campus;
import com.yuyou.fn.educational.entity.Room;
import com.yuyou.fn.educational.mapper.RoomMapper;
import com.yuyou.fn.educational.service.ICampusService;
import com.yuyou.fn.educational.service.IRoomService;
import com.yuyou.fn.educational.vo.RoomQueryVo;
import com.yuyou.fn.educational.vo.RoomSaveOrUpdateVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Room 表数据服务层接口实现类
 */
@Service
public class RoomServiceImpl extends SuperServiceImpl<RoomMapper, Room, Long> implements IRoomService  {

    @Resource
    private ICampusService campusService;



    @Override
    public void deleteByCampusId(Long campusId) {

        Example<Room> example = Room.createExample();
        example.selectProperties("roomId");
        example.createCriteria().andEqualTo("campusId", campusId);

        List<Room> roomList = findListByExample(example);

        for (Room room : roomList) {
            deleteRoom(room.getRoomId());
        }

    }

    @Override
    public void deleteRoom(Long roomId) {
        //// TODO: 2017/3/2  check

        deleteById(roomId);

    }

    @Override
    public Page<Room> findRoomByQueryForPage(RoomQueryVo roomQueryVo, int pageNo, int pageSize) {
        Example<Room> example = Room.createExample();
        example.orderBy("createTime").desc();
        Example.Criteria criteria = example.createCriteria();
        CriteriaUtils.fillCondition(criteria, roomQueryVo);
        return findPageByExample(example, pageNo, pageSize);

    }

    @Override
    public void saveOrUpdateRoom(RoomSaveOrUpdateVo roomSaveOrUpdateVo) {

        Room room = new Room();
        BeanUtils.copyProperties(room, roomSaveOrUpdateVo);
        checkRoomName(room);
        fillExtraData(room);
        if (room.getRoomId() == null) {
            room.setCreateTime(TimeUtils.now());
            room.setUpdateTime(TimeUtils.now());
            insertSelective(room);
        } else {
            room.setUpdateTime(TimeUtils.now());
            updateSelectiveById(room);
        }

    }

    private void fillExtraData(Room room) {
        Campus campus = campusService.findById(room.getCampusId());
        room.setCampusName(campus.getCampusName());
        room.setProvince(campus.getProvince());
        room.setCity(campus.getCity());
        room.setDistrict(campus.getDistrict());
    }

    private void checkRoomName(Room room) {
        Example<Room> example = Room.createExample();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("campusId",room.getCampusId()).andEqualTo("roomName" , room.getRoomName());
        Room one = findOneByExample(example);
        if(one != null &&(room.getRoomId() == null || !one.getRoomId().equals(room.getRoomId()))){
            throw new BusException("教室名重复");
        }
    }

    @Override
    public Page<Room> findRoomListForClassArrangement(RoomQueryVo roomQueryVo, int pageNo, int pageSize) {
        roomQueryVo.setStatus(1);
        return findRoomByQueryForPage(roomQueryVo, pageNo, pageSize);
    }

    @Override
    public List<Room> findListByCampusId(Long campusId, Long areaTeamId, Long busTeamId) {
        Example<Room> example = Room.createExample();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("campusId",campusId);
        CriteriaUtils.fillCondition(criteria,areaTeamId , busTeamId );
        return findListByExample(example);
    }
}